package 哈希表_有序表_比较器;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author ljb
 * @version 1.0
 * @Date 2024/11/24
 */
public class Code03_Comparator {

    public static class Employee{
        public int company;
        public int age;

        public Employee(int company, int age){
            this.company = company;
            this.age = age;
        }
    }

    public static class EmployeeComparator implements Comparator<Employee>{
        @Override
        public int compare(Employee o1, Employee o2) {
            // 任何比较器都默认
            // 如果返回负数认为o1的优先级更高
            // 如果返回正数认为o2的优先级更高
            // 任何比较器都是这样，所以利用这个设定，可以定制优先级怎么确定，也就是怎么比较
            // 不再有大小的概念，就是优先级的概念
            return o1.age - o2.age;
        }
    }

    public static void main(String[] args) {
        Employee emp1 = new Employee(2, 27);
        Employee emp2 = new Employee(1, 60);
        Employee emp3 = new Employee(4, 19);
        Employee emp4 = new Employee(3, 23);
        Employee emp5 = new Employee(1, 35);
        Employee emp6 = new Employee(3, 55);
        Employee[] arr = {emp1, emp2, emp3, emp4, emp5, emp6};
        Arrays.sort(arr, new EmployeeComparator());
        for (Employee employee : arr) {
            System.out.println(employee.company + " , " + employee.age);
        }

        System.out.println("=====================================");

        Arrays.sort(arr, (a, b) -> b.age - a.age);
        for (Employee employee : arr) {
            System.out.println(employee.company + " , " + employee.age);
        }

        System.out.println("=====================================");

        //  // 所有员工，先按照谁的公司编号小，谁在前；如果公司编号一样，谁年龄小谁在前
        Arrays.sort(arr, (a, b) -> a.company != b.company ? (a.company - b.company) : (a.age - b.age));
        for (Employee employee : arr) {
            System.out.println(employee.company + " , " + employee.age);
        }

        System.out.println("=====================================");

        TreeSet<Employee> treeSet = new TreeSet<>(new EmployeeComparator());
        for (Employee employee : arr) {
            treeSet.add(employee);
        }
        System.out.println(treeSet.size());
        for (Employee employee : treeSet) {
            System.out.println(employee.company + " , " + employee.age);
        }

        //会去重
        treeSet.add(new Employee(2, 27));
        System.out.println(treeSet.size());

        System.out.println("=====================================");

        // 如果不想去重，就需要增加更多的比较
        // 比如对象的内存地址、或者如果对象有数组下标之类的独特信息
        TreeSet<Employee> treeSet2
                = new TreeSet<>((a, b) -> a.company != b.company ? (a.company - b.company) : a.age != b.age ? (a.age - b.age) :a.toString().compareTo(b.toString()));
        for (Employee employee : arr) {
            treeSet2.add(employee);
        }
        System.out.println(treeSet2.size());
        for (Employee employee : treeSet2) {
            System.out.println(employee.company + " , " + employee.age + employee.toString());
        }
        //不会去重
        treeSet2.add(new Employee(2, 27));
        System.out.println(treeSet2.size());
        for (Employee employee : treeSet2) {
            System.out.println(employee.company + " , " + employee.age + employee.toString());
        }

        System.out.println("=====================================");


    //    字典序
        String str1 = "abcde";
        String str2 = "ks";
        System.out.println(str1.compareTo(str2));
        System.out.println(str2.compareTo(str1));
    }
}
